package com.xxxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xxxy.commonResult.CommonResult;
import com.xxxy.commonResult.CommonResultPage;
import com.xxxy.dao.StudentDao;
import com.xxxy.dao.TeachingClassStudentDao;
import com.xxxy.exception.MyException;
import com.xxxy.model.Student;
import com.xxxy.model.TeachingClassStudent;
import com.xxxy.response.StudentResponse;
import com.xxxy.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;

import java.util.List;

/**
 * @author sjh
 * @date 2022/1/23 12:13
 */
@Slf4j
@Service
public class StudentServiceImpl extends ServiceImpl<StudentDao, Student> implements StudentService {

    @Autowired
    StudentDao studentDao;

    @Autowired
    TeachingClassStudentDao teachingClassStudentDao;

    @Autowired
    private TransactionTemplate transactionTemplate;

    @Override
    public Student loginByStudent(String account, String pwd) {
        Student student =  studentDao.selectById(account);
        if(student == null){
            throw new MyException("用户名密码错误");
        }
        else if(!student.getPwd().equals(pwd)){
            throw new MyException("用户名密码错误");
        }
        else{
            return student;
        }
    }

    @Override
    public StudentResponse getByAccount(String account) {
        StudentResponse student = studentDao.getByAccount(account);
        if (student == null){
            throw new MyException("无该账号信息");
        }
        return student;
    }

    @Override
    public CommonResultPage list(Integer page, Integer size, Student student) {
        PageHelper.startPage(page,size);
        List<StudentResponse> studentList = studentDao.list(student);
        PageInfo<StudentResponse> pageInfo = new PageInfo<>(studentList);
        CommonResultPage result = new CommonResultPage();
        result.setTotal(pageInfo.getTotal());
        result.setData(pageInfo.getList());
        return result;
    }

    @Override
    public List<Student> list(Student student) {
        LambdaQueryWrapper<Student> lambdaQueryWrapper = Wrappers.lambdaQuery();
        if(student != null){
            if(student.getAccount() != null){
                lambdaQueryWrapper.eq(Student::getAccount, student.getAccount());
            }
            if(student.getName() != null){
                lambdaQueryWrapper.eq(Student::getName, student.getName());
            }
            if(student.getSex() != null){
                lambdaQueryWrapper.eq(Student::getSex, student.getSex());
            }
        }
        List<Student> studentList = studentDao.selectList(lambdaQueryWrapper);
        return studentList;
    }

    @Override
    public Boolean deleteStudent(String[] accountArray) {
        LambdaQueryWrapper<Student> lambdaQueryWrapper = Wrappers.lambdaQuery();
        LambdaQueryWrapper<TeachingClassStudent> lambdaQueryWrapper1 = Wrappers.lambdaQuery();
        for(String account: accountArray){
            lambdaQueryWrapper.eq(Student::getAccount, account);
            lambdaQueryWrapper.or();

            lambdaQueryWrapper1.eq(TeachingClassStudent::getStudentAccount, account);
            lambdaQueryWrapper1.or();
        }
        Boolean execute = transactionTemplate.execute(i -> {
            studentDao.delete(lambdaQueryWrapper);
            teachingClassStudentDao.delete(lambdaQueryWrapper1);
            return Boolean.TRUE;
        });
        return execute;
    }

    @Override
    public Boolean saveData(List<Student> students) {
        log.info("存储学生excel数据");
        boolean flag = this.saveBatch(students);
        if(flag){
            log.info("存储学生excel数据成功！");
            return true;
        }
        return false;
    }
}
